<?php


namespace app\zhongcan\controller;


use app\zhongcan\model\NewsModel;
use app\zhongcan\validate\NewsValidate;
use cmf\controller\AdminBaseController;

class NewsController extends AdminBaseController
{
    //页面显示
    public function index(){
        $keyword = input('keyword','');
        $where = [];
        if($keyword){
            $where[] = ['title|author', 'like', "%$keyword%"];
        }
        $list = NewsModel::order(['list_order'=>'DESC'])
            ->where($where)
            ->paginate(5);
        $this->assign('list',$list);
        return $this->fetch();
    }
    //添加页面显示
    public function add(){
        return $this->fetch();
    }
    //添加方法
    public function addPost()
    {
        $data = input('post.');
        $content1 = htmlspecialchars_decode($data['content']);
        $content2 = cmf_replace_content_file_url($content1, true);
        $content3 = htmlspecialchars($content2);
        $data['content'] = $content3;
        //验证数据
        $validate = new NewsValidate();
        $res = $validate->scene('add')->check($data);
        if (!$res) {
            $this->error($validate->getError());
        }
        $data['create_time'] = date('Y-m-d H:i:s');
        //入库
        if (NewsModel::create($data)) {
            $this->success('添加成功', 'index');
        } else {
            $this->error('添加失败');
        }
    }
        //编辑
        public function edit()
        {
            $id = input('id');
            $list = NewsModel::find($id);
            $list['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($list['content']));
            return $this->fetch('edit', [
                'list' => $list
            ]);
        }

        public function editPost()
        {
            $data = input('post.');
            //方式1
            $content1 = htmlspecialchars_decode($data['content']);
            $content2 = cmf_replace_content_file_url($content1, true);
            $content3 = htmlspecialchars($content2);
            $data['content'] = $content3;
            //验证器
            $validate = new NewsValidate();
            $result = $validate->scene('edit')->check($data);
            if (!$result) {
                $this->error($validate->getError());
            }
            $data['update_time'] = date('Y-m-d H:i:s');
            //查询
            $list = NewsModel::find($data['id']);
            if (!$list) {
                $this->error('未查询的记录');
            }
            $list->update_time = date('Y-m-d H:i:s');
            if ($list->update($data)) {
                $this->success('保存成功', 'News/index');
            } else {
                $this->error('保存失败');
            }
        }

    //单条删除
    public function delete()
    {
        $id = input('id/d');

        $del =NewsModel::destroy($id);
        //判断该id是否存在
        if (!$del) {
            $this->error('该数据不存在');
        }

        $del = NewsModel::destroy($id);
        if ($del) {
            $this->success('删除成功', 'News/index');
        } else {
            $this->error('删除失败');
        }
    }
    //排序
    public function order()
    {
        $model = new NewsModel();
        parent::listOrders($model);
        $this->success('排序成功');
    }

     //显示隐藏
    public function changeStatus()
    {
        $id = input('id/d');
        $news = NewsModel::find($id);
        if($news['switch']==0)
        {
            $newSwitch = 1;
        }else{
            $newSwitch = 0;
        }
        $news->switch = $newSwitch;
        if($news->save())
        {
            $this->success('成功');
        }else{
            $this->error('失败');
        }
    }
    //预览文章
    public function detail()
    {
        $id = input('id/d');
        $news = NewsModel::find($id);
        $news['content'] = cmf_replace_content_file_url(htmlspecialchars_decode($news['content']));
        $this->assign('list',$news);
        return $this->fetch('detail');
    }
}